package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.comon.ResultInfo;
import com.itheima.reggie.comon.UserHolder;
import com.itheima.reggie.domain.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    //查询购物车
    @GetMapping("/list")
    public ResultInfo<List<ShoppingCart>> findList() {
        List<ShoppingCart> cartList = shoppingCartService.list();
        return ResultInfo.success(cartList);
    }

    //添加到购物车
    @PostMapping("/add")
    public ResultInfo<ShoppingCart> addCart(@RequestBody ShoppingCart shoppingCart) {
        ShoppingCart newCart = shoppingCartService.addCart(shoppingCart);
        return ResultInfo.success(newCart);
    }

    //购物车--
    @PostMapping("/sub")
    public ResultInfo<ShoppingCart> subCart(@RequestBody ShoppingCart shoppingCart) {
        ShoppingCart newCart = shoppingCartService.subCart(shoppingCart);
        return ResultInfo.success(newCart);
    }

    //清空购物车
    @DeleteMapping("/clean")
    public ResultInfo<Object> cleanCart() {
        //根据用户id清空
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, UserHolder.get().getId());
        shoppingCartService.remove(wrapper);
        return ResultInfo.success(null);
    }
}
